package com.gzx.plugin.tjzy.modular.work.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.gzx.plugin.tjzy.modular.gzxcase.mapper.GzxCaseMapper;
import com.gzx.plugin.tjzy.modular.gzxcase.vo.GzxCaseVo;
import com.gzx.plugin.tjzy.modular.liaison.entity.GzxDebtorLiaison;
import com.gzx.plugin.tjzy.modular.liaison.mapper.GzxDebtorLiaisonMapper;
import com.gzx.plugin.tjzy.modular.sms.batch.service.GzxSmsService;
import com.gzx.plugin.tjzy.modular.work.entity.GzxMediatorWork;
import com.gzx.plugin.tjzy.modular.work.entity.bo.GzxMediatorWorkAddAndSmsBo;
import com.gzx.plugin.tjzy.modular.work.entity.bo.GzxMediatorWorkAddOrEditBo;
import com.gzx.plugin.tjzy.modular.work.entity.bo.GzxMediatorWorkQueryBo;
import com.gzx.plugin.tjzy.modular.work.entity.vo.GzxMediatorWorkVo;
import com.gzx.plugin.tjzy.modular.work.enums.GzxWorkType;
import com.gzx.plugin.tjzy.modular.work.mapper.GzxMediatorWorkMapper;
import com.gzx.plugin.tjzy.modular.work.service.GzxMediatorWorkService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.consts.IntConstant;
import vip.xiaonuo.common.util.CommonCryptogramUtil;
import vip.xiaonuo.common.util.CommonDateUtil;
import vip.xiaonuo.common.util.CommonSleepUtil;
import vip.xiaonuo.common.util.MapstructUtils;

import java.util.List;
import java.util.stream.Collectors;

import static org.apache.commons.lang.StringEscapeUtils.escapeSql;

/**
 * @author zjt
 * @description
 * @date 2024/12/5 11:05
 */
@Service
@DS("tjzy")
@RequiredArgsConstructor
public class GzxMediatorWorkServiceImpl implements GzxMediatorWorkService {

    private final GzxMediatorWorkMapper mediatorWorkMapper;

    private final GzxCaseMapper caseMapper;

    private final GzxDebtorLiaisonMapper debtorLiaisonMapper;

    private final GzxSmsService smsService;

    /**
     * 分页查询作业记录
     *
     * @param mediatorWorkQueryBo
     * @return
     */
    @Override
    public Page<GzxMediatorWorkVo> pageList(GzxMediatorWorkQueryBo mediatorWorkQueryBo) {
        return mediatorWorkMapper.selectVoPage(new Page<>(mediatorWorkQueryBo.getCurrent(), mediatorWorkQueryBo.getSize()), Wrappers.<GzxMediatorWork>lambdaQuery()
                .eq(GzxMediatorWork::getDebtorCardId, CommonCryptogramUtil.doSm1AesEncrypt(mediatorWorkQueryBo.getDebtorCardId()))
                .eq(GzxMediatorWork::getProjectCode, mediatorWorkQueryBo.getProjectCode())
                .orderByDesc(GzxMediatorWork::getId));
    }

    /**
     * 预测外呼自动记录催记
     *
     * @param mediatorWorkAddOrEditBo
     */
    @Override
    public GzxCaseVo autoAddByCaseId(GzxMediatorWorkAddOrEditBo mediatorWorkAddOrEditBo) {
        GzxCaseVo caseVo = caseMapper.selectVoById(mediatorWorkAddOrEditBo.getCaseId());
        if (caseVo != null) {
            GzxMediatorWork mediatorWork = new GzxMediatorWork();
            mediatorWork.setCaseId(Long.valueOf(caseVo.getId()));
            mediatorWork.setProjectCode(caseVo.getProjectCode());
            mediatorWork.setDebtorCardId(caseVo.getDebtorCardId());
            mediatorWork.setAffiliatedCompany(Long.valueOf(StpLoginUserUtil.getLoginUser().getOrgId()));
            mediatorWork.setLiaisonPhone(mediatorWorkAddOrEditBo.getLiaisonPhone());
            GzxDebtorLiaison debtorLiaison = debtorLiaisonMapper.selectOne(Wrappers.<GzxDebtorLiaison>lambdaQuery()
                    .eq(GzxDebtorLiaison::getDebtorCardId, caseVo.getDebtorCardId())
                    .eq(GzxDebtorLiaison::getLiaisonPhone, CommonCryptogramUtil.doSm1AesEncrypt(mediatorWorkAddOrEditBo.getLiaisonPhone())));
            if (debtorLiaison != null) {
                mediatorWork.setLiaisonName(debtorLiaison.getLiaisonName());
                mediatorWork.setCallRecipient(debtorLiaison.getRelationship());
                mediatorWork.setPhoneSource(debtorLiaison.getPhoneSource());
            }
            mediatorWork.setWorkType(GzxWorkType.OUTBOUND_PREDICTIVE.getCode());
            mediatorWork.setWorkResult(1);
            mediatorWorkMapper.insert(mediatorWork);
        }
        return caseVo;
    }

    /**
     * 作业记录新增
     *
     * @param mediatorWorkAddOrEditBo
     * @return
     */
    @Override
    public String add(GzxMediatorWorkAddOrEditBo mediatorWorkAddOrEditBo) {
        GzxMediatorWork mediatorWork = MapstructUtils.convert(mediatorWorkAddOrEditBo, GzxMediatorWork.class);
        mediatorWork.setAffiliatedCompany(Long.valueOf(StpLoginUserUtil.getLoginUser().getOrgId()));
        mediatorWorkMapper.insert(mediatorWork);
        return String.valueOf(mediatorWork.getId());
    }

    /**
     * 作业记录修改
     *
     * @param mediatorWorkAddOrEditBo
     */
    @Override
    public void edit(GzxMediatorWorkAddOrEditBo mediatorWorkAddOrEditBo) {
        GzxMediatorWork mediatorWork = MapstructUtils.convert(mediatorWorkAddOrEditBo, GzxMediatorWork.class);
        mediatorWork.setDebtorCardId(null);
        mediatorWork.setLiaisonPhone(null);
        mediatorWorkMapper.updateById(mediatorWork);
    }

    /**
     * 根据手机号查询上次作业记录的案件信息
     *
     * @param phone
     * @return
     */
    @Override
    public GzxCaseVo getLastCallCaseByPhone(String phone) {
        return null;
    }

    @Override
    public void deleteWork(GzxMediatorWorkAddOrEditBo mediatorWorkAddOrEditBo) {
        mediatorWorkMapper.deleteById(mediatorWorkAddOrEditBo.getId());
    }

    /**
     * 批量新增作业记录
     *
     * @param mediatorWorkList
     */
    @Override
    public void insertBatch(List<GzxMediatorWork> mediatorWorkList) {
        if (CollectionUtil.isNotEmpty(mediatorWorkList)) {
            List<List<GzxMediatorWork>> batchList = Lists.partition(mediatorWorkList, IntConstant.VALUE_1000);
            for (List<GzxMediatorWork> batch : batchList) {
                String sql = batch.stream()
                        .map(s -> String.format("('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
                                escapeSql(String.valueOf(s.getCaseId())),
                                escapeSql(CommonCryptogramUtil.doSm1AesEncrypt(s.getDebtorCardId())),
                                escapeSql(s.getProjectCode()),
                                escapeSql(CommonCryptogramUtil.doSm1AesEncrypt(s.getLiaisonPhone())),
                                escapeSql(String.valueOf(s.getWorkType())),
                                escapeSql(String.valueOf(s.getWorkResult())),
                                escapeSql(s.getLiaisonName()),
                                escapeSql(s.getCallRecipient()),
                                escapeSql(String.valueOf(s.getPhoneSource())),
                                escapeSql(String.valueOf(s.getAffiliatedCompany())),
                                escapeSql(s.getTenantId()),
                                escapeSql(String.valueOf(s.getCreateUser())),
                                escapeSql(CommonDateUtil.dateTimeToStr(s.getCreateTime()))))
                        .collect(Collectors.joining(", "));
                mediatorWorkMapper.insertBatchByValSql(sql.replace("'null'", "null"));
                CommonSleepUtil.sleep(IntConstant.VALUE_100);
            }
        }
    }

    /**
     * 保存催记并发送催记
     *
     * @param mediatorWorkAddAndSmsBo
     */
    @Override
    public void addWordBySms(GzxMediatorWorkAddAndSmsBo mediatorWorkAddAndSmsBo) {
        GzxDebtorLiaison debtorLiaison = debtorLiaisonMapper.selectById(mediatorWorkAddAndSmsBo.getLiaisonId());
//        SmsSendRespBo smsSendRespBo = smsService.sendSms(mediatorWorkAddAndSmsBo.getSupplierCode(), debtorLiaison.getLiaisonPhone(), mediatorWorkAddAndSmsBo.getSmsContent());
        GzxMediatorWork mediatorWork = new GzxMediatorWork();
//        mediatorWork.setMsgId(smsSendRespBo.getMid());
        mediatorWork.setSmsContent(mediatorWorkAddAndSmsBo.getSmsContent());
        mediatorWork.setCaseId(mediatorWorkAddAndSmsBo.getCaseId());
        mediatorWork.setWorkType(GzxWorkType.SMS.getCode());
        mediatorWork.setLiaisonName(debtorLiaison.getLiaisonName());
        mediatorWork.setCallRecipient(debtorLiaison.getRelationship());
        mediatorWork.setPhoneSource(debtorLiaison.getPhoneSource());
        mediatorWork.setLiaisonPhone(debtorLiaison.getLiaisonPhone());
        mediatorWork.setDebtorCardId(debtorLiaison.getDebtorCardId());
        mediatorWork.setAffiliatedCompany(Long.valueOf(StpLoginUserUtil.getLoginUser().getOrgId()));
        mediatorWorkMapper.insert(mediatorWork);
    }
}
